<template>
  <view class="list">
    <my_goods @click.native="toDetail(item.goods_id)" v-for="(item,index) in list" :key="index" :data="item"></my_goods>

  </view>
</template>

<script>
  export default {
    data() {
      return {
        query: '',
        list: [],
        pagenum: 1,
        pagesize: 10,
        // 是否正在加载其他数据
        isLoading:false,
        // 数据源是否加载完毕
        isFinish:false

      }
    },
    onLoad({
      key
    }) {
      this.query = key
      this.getList()
    },

    // 钩子函数 上拉加载
    onReachBottom() {
      // 请求下一页资源
      this.pagenum += 1
      this.getList()
    },
    // 下拉刷新
    onPullDownRefresh(){
      // 重置数据源
      this.list = []
      this.pagenum= 1
      this.isLoading = false
      this.isFinish = false
      // 重新发送请求
      this.getList(true)
      
      
    },
    methods: {
      // 跳转详情页
      toDetail(goods_id){
       uni.navigateTo({
         url:`/pages/goods_detail/main?goods_id=${goods_id}`
       })
      },
      async getList(isStop) {
        // 判断是否正在加载数据
        if(this.isLoading) return
        // 打开 请求加载状态
        this.isLoading = true
        const {
          data: {
            message,
            meta
          }
        } = await uni.$http.get('/goods/search', {
          query: this.query,
          pagenum: this.pagenum,
          pagesize: this.pagesize
        })
        if (meta.status !== 200) return uni.$showMsg()
        // this.list = message.goods
        // 新旧数据的拼接处理
        this.list = [...this.list,...message.goods]
        // 关闭 请求加载状态
        this.isLoading = false
        // console.log(this.list,'list');
        if(message.goods.length === 0){
          this.isFinish = true
          uni.$showMsg('没有更多了')
        }
        if(isStop) {
          // 关闭下拉结构
          uni.stopPullDownRefresh()
                  }
      }
    }
  }
</script>

<style lang="scss">
  .list {}
</style>
